/* 静态加载字典数据信息 4 select */
function initwordbook4select(wbtype, defaultvalue, selname, notresetsel) {
	var selobj;
	if (!$(selname) && !D(selname)) {
		alert("未找到字典数据项容器['" + selname + "']");
	} else {
		if ($(selname)) {
			selobj = $(selname);
		} else if (D(selname)) {
			selobj = D(selname);
		}

		if (selobj.type == "select-one" || selobj.type == "select-multiple") {
			var url = context
					+ "/system/wbutil.do?method=staticloadwblist&wbname="
					+ wbtype;

			var result = trim(sendRequest(url));

			var wblist = result != "" ? result.split(";") : null;

			if (!notresetsel) {
				selobj.options.length = 0;
			}

			if (wblist) {
				for (var i = 0; i < wblist.length; i++) {
					var item = wblist[i];
					var itemstr = item.split(",");
					var id = itemstr[0];
					var name = itemstr[1];

					var option = new Option(name, id);
					if (defaultvalue == id) {
						option.selected = true;
					}
					selobj.options.add(option);
				}
			}
		} else {
			alert("字典数据项容器['" + select + "']的类型有误,需要[select]");
		}
	}
}

var wbscript;
var _wbtype;

/* 静态初始化wordbook类型表单(使用于多个select表单加载同一字典数据) */
function script4wordbookselect(wbtype, defaultvalue, selname, notresetsel) {
	var selobj;
	if (!$(selname) && !D(selname)) {
		alert("未找到字典数据项容器['" + selname + "']");
	} else {
		if ($(selname)) {
			selobj = $(selname);
		} else if (D(selname)) {
			selobj = D(selname);
		}

		if (selobj.type == "select-one" || selobj.type == "select-multiple") {
			if (!wbscript) {
				_wbtype = wbtype;
				var url = context
						+ "/system/wbutil.do?method=staticloadwblist&wbname="
						+ wbtype;
				wbscript = trim(sendRequest(url));
			} else if (_wbtype != wbtype) {
				_wbtype = wbtype;
				var url = context
						+ "/system/wbutil.do?method=staticloadwblist&wbname="
						+ wbtype;
				wbscript = trim(sendRequest(url));
			}

			var wblist = wbscript != "" ? wbscript.split(";") : null;

			if (!notresetsel) {
				selobj.options.length = 0;
			}

			if (wblist) {
				for (var i = 0; i < wblist.length; i++) {
					var item = wblist[i];
					var itemstr = item.split(",");
					var id = itemstr[0];
					var name = itemstr[1];

					var option = new Option(name, id);
					if (defaultvalue == id) {
						option.selected = true;
					}
					selobj.options.add(option);
				}
			}
		} else {
			alert("字典数据项容器['" + selname + "']的类型有误,需要[select]");
		}
	}
}

function initwordbook4checkbox(wbtype, defaultvalue, selarea, selname,
		notresetsel, rad) {
	var selobj;

	if (!$(selarea) && !D(selarea)) {
		alert("未找到字典数据项容器['" + selarea + "']");
	} else {
		if ($(selarea)) {
			selobj = $(selarea);
		} else if (D(selarea)) {
			selobj = D(selarea);
		}

		var url = context + "/system/wbutil.do?method=staticloadwblist&wbname="
				+ wbtype;

		var result = trim(sendRequest(url));

		var wblist = result != "" ? result.split(";") : null;

		var str = "";

		var rad_ = rad ? rad : "checkbox";

		if (wblist) {
			for (var i = 0; i < wblist.length; i++) {
				var item = wblist[i];
				var itemstr = item.split(",");
				var id = itemstr[0];
				var name = itemstr[1];
				
				var checkedid = findInDefaultvalue(id, defaultvalue);

				var checked = (id == defaultvalue ? "checked" : "");

				str += name + "<input type=\"" + rad_ + "\" " + checked
						+ " iid=\"" + checkedid + "\" iname=\"" + name
						+ "\" name=\"" + selname + "\" value=\"" + id
						+ "\">&nbsp;";

				// selobj.appendChild(newCheckbox);
			}
		}

		selobj.innerHTML = str;
	}
}

function findInDefaultvalue(v, defaultvalue) {
	var t = "";
	var str = defaultvalue.split(",");
	for (var i = 0; i < str.length; i++) {
		if (str[i].substring(0, str[i].indexOf("#")) == v) {
			t = str[i].substring(str[i].indexOf("#") + 1);
			break;
		}
	}

	return t;
}